function main() {
    // 线索移交
    const self = this;
    debugger

    let treeSelect = self.$$("tree_panel_9u8epp").$$getSelection().selectedRows;
    if (treeSelect.length === 0) {
        self.$message.warn("请选择移交对象");
        return;
    }

    const taskId = self.$$getRouteView().$$getRenderElement().extraParams.taskId;
    if (!taskId) {
        self.$message.warn("请重新选择待移交的线索！");
        return;
    }

    let clueData = {
        taskId: taskId,
        objectId: treeSelect[0].id,
        objectName: treeSelect[0].name
    }

    let username = api.USER_INFO().name;

    let handleRecord = {
        replyId: taskId,
        recordType: 1,
        recordTitle: "线索登记",
        recordContent: `督办人员${username}登记了个案线索。`,
        relationReplyId: ""
    }

    insertClueObject(clueData).then(res => {
        // 修改law_std_sup_clue.status 2=已移交
        insertHandleRecord(handleRecord).then(r => {
            updateClueStatus(taskId);
            notifyParentAndClose(self)
        })

    })

}

/**
 * 修改线索状态
 */
function updateClueStatus(taskId) {

    const law_std_sup_clue = "ds_tbl:t_560875aad5054fa99e5e9cdd1c703828";
    api.EXEC_FORMULA("UPDATE('" + law_std_sup_clue + "', QUERY_EQ('id', '" + taskId + "'), '', ['status', '" + 2 + "'])").then(() => {

        console.log("res", "执行结果,修改成功！");
        let pageListEle = window.cluePageList;
        // 获取列表渲染页实例
        const listRenderView = pageListEle.$$getRenderElement();
        // 获取当前显示的组件，表格或者卡片
        const currentListEle = listRenderView.$$getListElement();
        // 调用刷新方法
        currentListEle.$$refreshData();

    }).catch(err => {
        self.$message.error(err.message); // 显示错误提示信息
    });
}

/**
 * 处理经过
 * @param recordData
 * @returns {Promise<unknown>}
 */
function insertHandleRecord(recordData) {

    const insertHandleRecords = [
        ['record_uid', 'UUID()'],
        ['relation_id', `'${recordData.replyId}'`],
        ['record_type', `'${recordData.recordType}'`],
        ['record_title', `'${recordData.recordTitle}'`],
        ['record_content', `'${recordData.recordContent}'`],
        ['create_time', 'NOW()'],
        ['create_human_id', 'USER_ID()'],
        ['relation_reply_id', `'${recordData.relationReplyId}'`]
    ];

    const formattedFields = insertHandleRecords.map(([key, value]) => {
        return `'${key}',${value}`;
    });

    const law_std_sup_handle_record = "ds_tbl:t_0ff47ea0e3a74ffdbc274409e7103e7e";
    const insertSql = `INSERT('${law_std_sup_handle_record}', [${formattedFields.join(',')}])`;
    console.log(insertSql);


    return new Promise((resolve, reject) => {
        api.EXEC_FORMULA(insertSql).then(res => {
            console.log(res);
            resolve(res);
        })
    })

}

/**
 * 新增移交线索
 * @param clueData
 */
function insertClueObject(clueData) {

    const insertClueObjects = [
        ['task_id', clueData.taskId],
        ['opt_human_id', 'USER_ID()'],
        ['lead_unit_id', 'DEPARTMENT_ID()'],
        ['object_id', `'${clueData.objectId}'`],
        ['object_name', `'${clueData.objectName}'`],
        ['object_type', '1'],
        ['receive_status', '0'],
        ['finish_status', '0'],
        ['delete_flag', `'0'`],
        ['create_time', 'NOW()'],
        ['create_human_id', 'USER_ID()'],
    ];

    const formattedFields = insertClueObjects.map(([key, value]) => {
        return `'${key}',${value}`;
    });

    const law_std_sup_clue_object = "ds_tbl:t_dff1815b4c164b70a4c208ecf3e1506b";
    const insertSql = `INSERT('${law_std_sup_clue_object}', [${formattedFields.join(',')}])`;
    console.log(insertSql);


    return new Promise((resolve, reject) => {
        api.EXEC_FORMULA(insertSql).then(res => {
            console.log(res);
            resolve(res);
        })
    })

}

function notifyParentAndClose(self) {
    const pageRenderElement = self.$$root().$parent.$parent.$parent;
    if (window.parent) {
        window.parent.postMessage(
            JSON.stringify({event: "close-dialog", param: {XXX: "test"}}),
            "*"
        );
    }
}
